import Cookies from 'js-cookie'

const TOKEN_KEY = 'lt_token'

export default {
  isLogin() {
    return Cookies.get('_isLogin');
  },
  redirect() {
    let current = window.location.href;
    window.location.href = `/`;
  },
  delCookie(name) { 
      var exp = new Date(); 
      exp.setTime(exp.getTime() - 1); 
      var cval=this.getCookie(name); 
      if(cval!=null) 
          document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
  },
  getToken() {
      const token = Cookies.get(TOKEN_KEY)
      if (token) return token
      else return false 
  }, 
  getCookie(name) { 
      var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
      if(arr=document.cookie.match(reg))
          return unescape(arr[2]); 
      else 
          return null; 
  }, 
  getSub(datas, index){
    let returnArr = [];
    datas.forEach( ( data, i ) => {
      if (i != index) {
        returnArr.push({
          icon: data.icon,
          text: data.text,
          link: data.link,
          toActive: data.toActive,
        });
      }
    });
    return returnArr;
  }, 
  checkHas(json, datas){
    let bools = false;
    for (let data of datas) {
      if (json.hasOwnProperty(data)) {
        bools = true;
      }
    }
    return bools;
  }, 
}
